package indi.jp.project.core.dao.impl;

import indi.jp.project.core.dao.BaseDao;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Repository
@Primary
public class IBaseDao<T> implements BaseDao<T> {

    @Resource
    protected SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<T> queryList(String mapper, Object parameter) {
        return sqlSessionTemplate.selectList(mapper, parameter);
    }

    @Override
    public int queryCount(String mapper, Object parameter) {
        return sqlSessionTemplate.selectOne(mapper, parameter);
    }

    @Override
    public int insert(String mapper, Object parameter) {
        return sqlSessionTemplate.insert(mapper, parameter);
    }

    @Override
    public int batchInsert(String mapper, Object parameter) {
        return 0;
    }

    @Override
    public int update(String mapper, Object parameter) {
        return sqlSessionTemplate.update(mapper, parameter);
    }

    @Override
    public int delete(String mapper, Object parameter) {
        return sqlSessionTemplate.delete(mapper, parameter);
    }

    @Override
    @SuppressWarnings("unchecked")
    public T findById(String mapper, String id) {
        return (T)sqlSessionTemplate.selectOne(mapper, id);
    }

    @Override
    public int deleteById(String mapper, String id) {
        return sqlSessionTemplate.delete(mapper, id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public T findByObject(String mapper, Object parameter) throws SQLException {
        return (T)sqlSessionTemplate.selectOne(mapper, parameter);
    }

}
